package com.example.system.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.common.constant.ActivationConstant;
import com.example.common.constant.RedisConstant;
import com.example.common.utils.StringUtils;
import com.example.system.domain.Activation;
import com.example.system.domain.Role;
import com.example.system.domain.UserRole;
import com.example.system.mapper.ActivationMapper;
import com.example.system.mapper.RoleMapper;
import com.example.system.mapper.UserRoleMapper;
import com.example.system.service.ActivationService;
import com.example.system.service.DictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

@Service
public class ActivationServiceImpl extends ServiceImpl<ActivationMapper, Activation> implements ActivationService {
    @Autowired
    private ActivationMapper activationMapper;
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private UserRoleMapper userRoleMapper;
    @Autowired
    private DictService dictService;

    /**
     * 更新活跃度
     */
    @Async
    @Override
    public void updateActivation(Integer userId, String type) {
        LambdaQueryWrapper<Activation> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Activation::getUserId, userId);
        Activation activation = activationMapper.selectOne(lqw);
        if (StringUtils.isEmpty(activation)) {
            String key = RedisConstant.db_activation;
            int id = dictService.getId(key);
            activation = new Activation();
            activation.setUserId(userId);
            activation.setId(id);
            activationMapper.insert(activation);
            dictService.updateDBId(key);
        }
        //登录活跃度
        double login = activation.getLogin();
        if (ActivationConstant.Login.equals(type)) {
            login += ActivationConstant.ACTIVATION_Login;
            activation.setLogin(login);
        }
        //创建项目活跃度
        double addItem = activation.getAddItem();
        if (ActivationConstant.AddItem.equals(type)) {
            addItem += ActivationConstant.ACTIVATION_AddItem;
            activation.setAddItem(addItem);
        }
        //审核项目活跃度
        double process = activation.getProcess();
        if (ActivationConstant.Process.equals(type)) {
            process += ActivationConstant.ACTIVATION_Process;
            activation.setProcess(process);
        }
        //评分活跃度
        double score = activation.getScoreProject();
        if ("Score".equals(type)) {
            score += ActivationConstant.ACTIVATION_Score;
            activation.setScoreProject(score);
        }

        //总活跃度
        double all = login + addItem + process + score;
        activation.setAllActivation(all);
        activationMapper.updateById(activation);

        //获取当前用户权限
        Role role = roleMapper.getRoleByUserId(userId);
        int access = role.getAccess();
        if (all >= role.getActivation() && "YES".equals(role.getByAct())) {
            //活跃度足够，升级权限
            LambdaQueryWrapper<UserRole> lqw1 = new LambdaQueryWrapper<>();
            lqw1.eq(UserRole::getUserId, userId);
            UserRole userRole = userRoleMapper.selectOne(lqw1);
            Role role1 = roleMapper.getByAccess(--access);
            while (StringUtils.isEmpty(role1)) {
                role1 = roleMapper.getByAccess(--access);
            }
            userRole.setRoleId(role1.getRoleId());
            userRoleMapper.updateById(userRole);
            // TODO 更新用户登录信息
        }
    }
}
